home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Science⁄Math / Scientist's Helper src / s.helper.4 / regprim.c < prev   
C/C++ Source or Header  |  1986-02-06  |  14KB  |  578 lines

  1. #include "all.h"
  2. #include "regtabext.h"
  3.  
  4. errfcn( x, erf,erfc,erfci,erfci2, deriv )
  5. float x, *erf,*erfc,*erfci,*erfci2, *deriv;
  6. /* ERROR FUNCTION AND ASSOCIATED FUNCTIONS                */
  7. /*           X - ARGUMENT OF ERROR FUNCTION. POSITIVE OR NEGATIVE    */
  8.  
  9. /*                             X                    */
  10. /*           ERF = 2/SQRT(PI) S  EXP( - T**2 ) DT            */
  11. /*                             0                    */
  12.  
  13. /*           ERFC = 1.0 - ERF                        */
  14. /*           ERFCI IS  IERFC(X)                        */
  15. /*           ERFCI =  (1.0/SQRT(PI))*EXP(-X**2)-X(ERFC(X))        */
  16.  
  17. /*           ERFCI2 IS I**2ERFC(X)                    */
  18. /*                 ERFCI2 =  1/4 ((1+2*X**2)ERFC(X)- 2/SQRT(PI)*X*EXP(-X**2))    */
  19. /*                 OR  ALTERNATIVELY                        */
  20. /*                 ERFCI2 = 1/4 (ERCF(X)-2*X*ERFCI(X))                */
  21.  
  22. /*           DERIV - D/DX OF ERF(X)                        */
  23.  
  24. /* METHOD:      RATIONAL APPROXIMATION                        */
  25.  
  26. /* SOURCE:   ABRAMOWITZ AND STEGUN, P.299, HANDBOOK OF                */
  27. /*           MATHEMATICAL FUNCTIONS AND CARSLAW AND JAEGER            */
  28. /*           CONDUCTION OF HEAT IN SOLIDS  P. 482                */
  29. {
  30.     double t, xx, rat, dexpxx;
  31.     double derf, dderiv, derfc, derfci, derfci2;
  32.     double t2,t3,t4;
  33.       
  34.     double p=0.3275911, a1=0.254829592, a2=-0.284496736;
  35.     double a3=1.421413741, a4=-1.453152027, a5=1.061405429;
  36.     double sqrtpi=1.772453851, pi=3.141592654;
  37.  
  38.     xx = fabs( (double) x);
  39.     dexpxx = exp( -(xx*xx) );
  40.     if( xx<1.0e-7 ) { /*use first two terms of taylor series expansion*/
  41.         derf = (2.0 / sqrtpi) * xx * (1.0 - (xx*xx/3.0) );
  42.         if( x<0.0 ) derf=(-derf);
  43.         derfc = 1.0 - derf;
  44.         }
  45.     else { /*use rational approximation*/
  46.         t = 1.0 / ( 1.0 + p*xx );
  47.         t2 = t*t;
  48.         t3 = t2*t;
  49.         t4 = t2*t2;
  50.         rat = (a5*t4 + a4*t3 + a3*t2 + a2*t + a1);
  51.         derf = 1.0 - t*rat*dexpxx;
  52.         if (x<0.0) {
  53.             derf = (-derf);
  54.             derfc =  2.0 - t*rat*dexpxx;
  55.             }
  56.         else {
  57.             derfc =  t*rat*dexpxx;
  58.             } /*end if*/
  59.         } /*end if*/
  60.     xx = (double) x;
  61.     derfci = (1.0/sqrtpi)*dexpxx-xx*derfc;
  62.     derfci2 = 0.25 * (derfc-2.0*xx*derfci);
  63.     dderiv =  (2.0*dexpxx)/sqrtpi;
  64.  
  65.     *erf = (float) ( derf );
  66.     *erfc = (float) ( derfc );
  67.     *erfci = (float)  (derfci);
  68.     *erfci2 =  (float)  (derfci2);
  69.     *deriv = (float) ( dderiv );
  70.  
  71.  
  72. ToNaN(f)
  73. float *f;
  74. {
  75.     if( (errno==EDOM) || (errno==ERANGE) ) {
  76.         *f=infinity;
  77.         }
  78. }
  79. SetVar( vName, vValue )
  80. char vName[], vValue[];
  81. /*create entry in macro substitution table and set value*/
  82. /*if variable previously exists, it is overwritten */
  83. /* returns false if the list is full or if not a valid variable name */
  84. /* valid variable names must not contain spaces */
  85.  
  86. {
  87.     int i, inList;
  88.     char *index();
  89.                 
  90.     if (  (strlen(vName)==0) || ((long)index(vName, ' ')!=0L) ) { 
  91.         return(FALSE);
  92.         }
  93.     else {
  94.         inList = FALSE;
  95.         i = 0;
  96.         while (  (!inList) && (i<macVars.numVars) )  {                   
  97.             if ( strcmp(vName,macVars.inStr[i]) == 0 ) {
  98.                 inList = TRUE;
  99.                 } /*end if inList*/
  100.             i++;
  101.             } /*end while*/
  102.         if (inList) { 
  103.             strcpy( macVars.outStr[i-1], vValue );
  104.             return( TRUE );
  105.             }      
  106.         else {
  107.             if ( (macVars.numVars+1) >= maxVars ) {
  108.                 return( FALSE );
  109.                 }
  110.             else {                   
  111.                 strcpy( macVars.inStr[macVars.numVars], vName );
  112.                 strcpy( macVars.outStr[macVars.numVars], vValue );
  113.                 macVars.numVars++;
  114.                 return( TRUE );
  115.                 } /*end if enough space*/
  116.             } /*end if not inList*/
  117.         } /*end if valid name*/
  118.     }
  119.             
  120. DelVar( vName )
  121. char vName[];
  122. /* deletes macro substitution variable from table*/
  123. {
  124.  
  125.     int i, j, inList; 
  126.     
  127.     if (macVars.numVars==0) {
  128.         return( FALSE );
  129.         }
  130.     else {                   
  131.         inList  = FALSE;
  132.         i  = 0;
  133.         while ( (!inList) && (i<macVars.numVars)  ) {                   
  134.             if ( strcmp(vName,macVars.inStr[i])==0 ) {
  135.                 inList = TRUE;
  136.                 } /*end if match*/
  137.             i++;
  138.             }
  139.         i--; /*i now points to selected variable*/
  140.         if (inList) { 
  141.             if (i != (macVars.numVars-1) ) { /*if not last in list, move others*/             
  142.                 for ( j=(i+1); j<macVars.numVars; j++ ) {
  143.                     strcpy( macVars.inStr[j-1],   macVars.inStr[j]   );
  144.                     strcpy( macVars.outStr[j-1], macVars.outStr[j] );
  145.                     }
  146.                 } /*end if*/
  147.             macVars.numVars--;
  148.             return( TRUE );
  149.             } /*end if inList*/                 
  150.         else {
  151.             return( FALSE );
  152.             } /*end if not inList */
  153.         } /*end if list not empty*/
  154. }
  155.  
  156. ListVars()
  157. /* list macro substitution table */
  158. {
  159.     int i;
  160.     
  161.     for( i=0; i<macVars.numVars; i++ ) {                   
  162.         CheckAbortMenu();
  163.         WritePhrase( "\'" );
  164.         WritePhrase( macVars.inStr[i] );
  165.         WritePhrase( "\' \'" );
  166.         WritePhrase( macVars.outStr[i] );
  167.         WriteLine( "\'" );
  168.         } /* end for */
  169. }
  170.  
  171. SBreak( command, macrosOn )
  172. commandRec *command;
  173. int macrosOn;
  174. /* breaks command string into command words that are separated by blanks */
  175. /* quoted blanks not broken */
  176. /* macro substitutions performed on all command words if macrosOn true */
  177. {
  178.     int i, j, ibuf, len;
  179.     char c[2];
  180.     int quote, word, error, status;
  181.  
  182.        for(  i=0;  i<numCmdWds;  i++ ) {                   
  183.         strcpy( command->cmdWord[i], "" );  /*set command words to null*/
  184.         }
  185.  
  186.     i  = 0;
  187.     j  = 0;
  188.     quote = FALSE;
  189.     word  = FALSE;
  190.     error  = FALSE;
  191.     ibuf  =  0;
  192.     if( (len=strlen(command->cmdStr))==0) {
  193.         return(TRUE);
  194.         }
  195.  
  196.     c[1] = '\0';
  197.     while ( (ibuf<len) && (!error) ) {                   
  198.         if ( i >= cmdWordLen ) {
  199.             error  = TRUE;
  200.             }                   
  201.         else {  
  202.                      c[0] = command->cmdStr[ibuf];
  203.             if( j >= numCmdWds ) {
  204.                 error = TRUE;
  205.                 }
  206.                     else if ( ((c[0]==' ')||(c[0]=='\t')) && (!quote) ) {                   
  207.                 if (word)  {                   
  208.                     word  = FALSE;
  209.                     i  =  0;
  210.                     j++;
  211.                     }
  212.                 }
  213.                     else if ( (c[0]==' ')||(c[0]=='\t') && quote ) {                   
  214.                 word  = TRUE;
  215.                 strcat( command->cmdWord[j], c );
  216.                 i++;
  217.                 }
  218.                      else if ( (c[0]=='\'') && (!quote) ) {                   
  219.                 word  = TRUE;
  220.                 quote  = TRUE;
  221.                 }
  222.                      else if ( (c[0]=='\'') && quote )  {                   
  223.                 word = FALSE;
  224.                 quote = FALSE;
  225.                 i = 0;
  226.                 j++;
  227.                 }
  228.                      else {                   
  229.                 word  = TRUE;
  230.                 strcat( command->cmdWord[j], c ); 
  231.                 i++;
  232.                 } /*end if on characters*/;
  233.                      }  /*end if cmdWordLen*/
  234.             ibuf++;
  235.         } /*end while*/;
  236.     
  237.     status = FALSE;
  238.     if ( !error ) {
  239.         i = 0;
  240.         status = TRUE;
  241.         while ( (i<numCmdWds) && status && macrosOn )  {                   
  242.             status  = GetVar( command->cmdWord[i], command->cmdWord[i] );
  243.             i++;
  244.             } /*while numCmdWds*/;
  245.         } /*if not error*/
  246.         
  247.     return(  (!error) && status );
  248. }
  249.  
  250. GetVar( vName,  vValue)
  251. char vName[], vValue[];
  252. /* does macro-substitution on a variable reference*/
  253. /* does not change the value of VValue on no-match*/
  254. /* returns false only if no match occurs on a valid variable name*/
  255. /* where a valid variable reference*/
  256. /* a) starts with a @ */
  257. /* b) contains no spaces */
  258.  
  259. {
  260.     int i, len;
  261.     int inList;
  262.     char *index();
  263.     
  264.     if( ((long)index(vName,' ')!=0L) || ((long)index(vName,'@')!=(long)vName) ) { 
  265.         /*not a variable reference*/
  266.         return(TRUE);
  267.         }
  268.     else if ( (len=strlen(vName)) < 2 ) { /*string consisting only of @ is invalid*/
  269.         return(FALSE);
  270.         }
  271.     else if (macVars.numVars==0) {  /*is valid reference but no variables defined*/
  272.         return(FALSE);
  273.         }                   
  274.     else {  /* is a valid variable reference */
  275.         inList  = FALSE;
  276.         i  = 0;
  277.         while ( (!inList) && (i<macVars.numVars) ) {                   
  278.             if (strcmp( &(vName[1]) , macVars.inStr[i] ) == 0) {
  279.                 inList = TRUE;
  280.                 } /*end if*/
  281.                      i++; 
  282.                      }  /*end while*/
  283.         if (inList)  {
  284.                      strcpy( vValue, macVars.outStr[i-1] );
  285.              return(TRUE);
  286.                      } /*end if inList*/                 
  287.         else {                   
  288.             return(FALSE);
  289.             } /*end if not inList*/
  290.         } /* end if valid reference*/
  291.  
  292. done()
  293. {
  294.     if( doneFlag ) {
  295.         if( CautionAlert(quitAlertRes, NULL)==1 ) {
  296.             return(TRUE);
  297.             }
  298.         else {
  299.             return(FALSE);
  300.             }
  301.     }
  302.     else {
  303.         return(FALSE);
  304.         }
  305. }
  306.  
  307. GoodRow( row ) /* returns 0 if 1<=row<=rows,  1 if rows<row<=maxrows, 2 otherwise */
  308. int row;
  309. {
  310.     if( (row>=1) && (row<=table.header.rows) ) {
  311.         return(0);
  312.         }
  313.     else if( (row>table.header.rows) && (row<=table.header.maxRows) ) {
  314.         return(1);
  315.         }
  316.     else {
  317.         return(2);
  318.         }
  319. }
  320.  
  321. GoodCol( col ) /* returns 0 if 1<=col<=cols,  1 if cols<col<=maxcols, 2 otherwise */
  322. int col;
  323. {
  324.     if( (col>=1) && (col<=table.header.cols) ) {
  325.         return(0);
  326.         }
  327.     else if( (col>table.header.cols) && (col<=table.header.maxCols) ) {
  328.         return(1);
  329.         }
  330.     else {
  331.         return(2);
  332.         }
  333. }
  334.  
  335. ErrMsg( message )
  336. char message[];
  337. {
  338.     int i;
  339.     char s[80];
  340.     
  341.     HiliteMenu(0);
  342.     RedoEditWindow();
  343.     if( currentWindow!=coWindow ) {
  344.         SetPort(theWindow[coWindow]);
  345.         SelectWindow( theWindow[coWindow] );
  346.         currentWindow = coWindow;
  347.         whichWindow = theWindow[coWindow];
  348.         }
  349.     (*coText)->selStart = (*coText)->teLength;
  350.     (*coText)->selEnd = (*coText)->teLength;
  351.     TEInsert( "Error: ", 7L, coText );
  352.     TEInsert( message, (long)strlen(message), coText );
  353.     TEInsert( "\r", 1L, coText );
  354.     if( mem.active ) { /*turn off procedure*/
  355.         TESetSelect( (long)((*prText)->lineStarts[mem.stack[mem.stackPtr]]),
  356.                  (long)((*prText)->lineStarts[mem.stack[mem.stackPtr]+1]),
  357.                  prText );
  358.         pendingFlag=FALSE;
  359.         NoPendingInput();
  360.         for (i=mem.stackPtr; i>=0; i-- ) {
  361.             IToS( mem.stack[i]+1, s );
  362.             TEInsert("called from line ",17L,coText);
  363.             TEInsert(s, (long)strlen(s), coText );
  364.             TEInsert("\r", 1L, coText);
  365.             }
  366.         mem.active=FALSE;
  367.         mem.stackPtr=-1;
  368.         loops.numLoops=0;
  369.         }
  370.     TEInsert( "> ", 2L, coText );
  371.     IfOutScroll( coText );
  372.     SysBeep(5);
  373.     longjmp(envbuf,-1);
  374. }
  375.  
  376. WriteLine( message )
  377. char message[];
  378. {
  379.     if( currentWindow!=coWindow ) {
  380.         SetPort(theWindow[coWindow]);
  381.         SelectWindow( theWindow[coWindow] );
  382.         currentWindow = coWindow;
  383.         whichWindow = theWindow[coWindow];
  384.         }
  385.     (*coText)->selStart = (*coText)->teLength;
  386.     (*coText)->selEnd = (*coText)->teLength;
  387.     TEInsert( &message[0], (long)strlen(message), coText );
  388.     TEInsert( "\r", 1L, coText );
  389.     IfOutScroll( coText );
  390. }
  391.  
  392. WritePhrase( message )
  393. char message[];
  394. {
  395.     if( currentWindow!=coWindow ) {
  396.         SetPort(theWindow[coWindow]);
  397.         SelectWindow( theWindow[coWindow] );
  398.         currentWindow = coWindow;
  399.         whichWindow = theWindow[coWindow];
  400.         }
  401.     (*coText)->selStart = (*coText)->teLength;
  402.     (*coText)->selEnd = (*coText)->teLength;
  403.     TEInsert( &message[0], (long)strlen(message), coText );
  404.     IfOutScroll( coText );
  405. }
  406.  
  407.  
  408. CheckAbortMenu()
  409. {
  410.     Point mPoint;
  411.     if (Button() != 0) {
  412.         GetMouse( &mPoint );
  413.         LocalToGlobal( &mPoint );
  414.             if( PtInRect( pass(mPoint), &abortRect ) != 0 ) {
  415.                 SysBeep(20);
  416.                 SetPort(theWindow[coWindow]);
  417.                 SelectWindow( theWindow[coWindow] );
  418.                 currentWindow = coWindow;
  419.                 whichWindow = theWindow[coWindow];
  420.                 if( mem.active ) { /*turn off procedure*/
  421.                     mem.active=FALSE;
  422.                     mem.stackPtr=-1;
  423.                     loops.numLoops=0;
  424.                     pendingFlag=FALSE;
  425.                     NoPendingInput();
  426.                     }
  427.                 TEInsert( "Abort!\r",7L,coText);
  428.                 TEInsert( "> ", 2L, coText );
  429.                 IfOutScroll( coText );
  430.                 RedoEditWindow();
  431.                 HiliteMenu(0);
  432.                 longjmp(envbuf,-1);
  433.                 } /*end if abort menu and item*/
  434.         } /*end if mouse is down*/
  435.     HiliteMenu(0);
  436. }
  437. PendingInput()
  438. {
  439.     DisableItem( myMenu[apMenu], 0 );
  440.     DisableItem( myMenu[fiMenu], 1 );
  441.     DisableItem( myMenu[fiMenu], 4 );
  442.     DisableItem( myMenu[fiMenu], 7 );
  443.     DisableItem( myMenu[edMenu], 0 );
  444.     DisableItem( myMenu[wiMenu], 3 );
  445.     DisableItem( myMenu[wiMenu], 4 );
  446. }
  447.  
  448. NoPendingInput()
  449. {
  450.     EnableItem( myMenu[apMenu], 0 );
  451.     EnableItem( myMenu[fiMenu], 1 );
  452.     EnableItem( myMenu[fiMenu], 4 );
  453.     EnableItem( myMenu[fiMenu], 7 );
  454.     EnableItem( myMenu[edMenu], 0 );
  455.     EnableItem( myMenu[wiMenu], 3 );
  456.     EnableItem( myMenu[wiMenu], 4 );
  457. }
  458.  
  459. Graph2Vars()
  460. {
  461.     int status;
  462.     char s[cmdWordLen];
  463.     
  464.     RToS( graph.xMin, s );
  465.     status = SetVar( "xmin", s );
  466.     
  467.     RToS( graph.xMax, s );
  468.     status = status && SetVar( "xmax", s );
  469.     
  470.     RToS( graph.yMin, s );
  471.     status = status && SetVar( "ymin", s );
  472.     
  473.     RToS( graph.yMax, s );
  474.     status = status && SetVar( "ymax", s );
  475.     
  476.     if( !status ) {
  477.         ErrMsg("couldnt create graphics variables");
  478.         }
  479. }
  480.  
  481. FindLabel( s, line) /*search for particular label and return its line number*/
  482. char s[];
  483. int *line;
  484. {
  485.     commandRec tCommand;
  486.     int i, j, k, match;
  487.     
  488.     match = FALSE;
  489.     *line = 0;
  490.     for( i=0; (i<mem.numLabels && (!match)); i++) { /*search label list*/
  491.         j=(*prText)->lineStarts[mem.labels[i]];
  492.         k=((*prText)->teLength)-1;
  493.         HLock((*prText)->hText);
  494.         ExtractLine( *((*prText)->hText), j, k, tCommand.cmdStr );
  495.         HUnlock((*prText)->hText);
  496.         if( !SBreak( &tCommand, FALSE ) ) {
  497.             WriteLine("(find) bad line in procedure:");
  498.             /*ErrMsg(tCommand.cmdStr);*/
  499.             }
  500.         if (strcmp(tCommand.cmdWord[1],s)==0) {
  501.             match = TRUE;
  502.             *line  = mem.labels[i];
  503.             }
  504.         } /*end for*/
  505.     return( match );
  506. }
  507.  
  508. ListLabels() /*constructs label list from current contents of procedure memory*/
  509. {
  510.     commandRec tCommand;
  511.     int i, j, k, badLine;
  512.     
  513.     mem.numLabels = 0;
  514.     badLine=FALSE;
  515.     for( i=0; i<(*prText)->nLines; i++) {/*scan memory sequentially*/
  516.         j=(*prText)->lineStarts[i];
  517.         k=((*prText)->teLength)-1;
  518.         HLock((*prText)->hText);
  519.         ExtractLine( *((*prText)->hText), j, k, tCommand.cmdStr );
  520.         HUnlock((*prText)->hText);
  521.         if( !SBreak( &tCommand, FALSE ) ) {
  522.             TESetSelect( (long)((*prText)->lineStarts[i]), (long)((*prText)->lineStarts[i+1]),
  523.                  prText );
  524.             WriteLine("bad line in procedure:");
  525.             WriteLine(tCommand.cmdStr);
  526.             badLine=TRUE;
  527.             }
  528.         if( strcmp(tCommand.cmdWord[0],"label")==0 ) {
  529.             mem.labels[mem.numLabels] = i;
  530.             mem.numLabels++;
  531.             }
  532.         } /*end for*/
  533.     if( badLine ) {
  534.         ErrMsg("procedure aborted");
  535.         }
  536. }
  537.  
  538. ExtractLine(c, start, end, cc)
  539. char c[];
  540. int start, end;
  541. char cc[];
  542. {
  543.     int i;
  544.     for( i=0; ( (i<(end-start+1)) && (i<(cmdWordLen-1)) ); i++ ) {
  545.         cc[i] = c[i+start];
  546.         if (cc[i]=='\r') {
  547.             cc[i] = '\0';
  548.             break;
  549.             }
  550.         }
  551.     cc[i] = '\0';
  552. }
  553.  
  554. RedoEditWindow()
  555. {
  556.     GrafPtr oldPort;
  557.     int oldWindow;
  558.     
  559.     GetPort( &oldPort );
  560.     oldWindow=currentWindow;
  561.     
  562.     SetPort( theWindow[edWindow] );
  563.     InvalRect( &(theWindow[edWindow]->portRect) );
  564.     tabEd.activeName=FALSE;
  565.     tabEd.activeEntry=FALSE;
  566.     
  567.     SetPort( oldPort );
  568.     currentWindow=oldWindow;
  569.     whichWindow=theWindow[currentWindow];
  570. }
  571.  
  572.  
  573.  
  574.  
  575.  
  576.